/// <summary>
/// UV位移
/// </summary>
using UnityEngine;
using System.Collections;

public enum UV_MODE_PLAY_MODE
{
	NONE,
	ONLY_X,
	ONLY_Y,
}

/*
public struct UVMoveStruct
{
	public float playSpeed;
	public UV_MODE_PLAY_MODE playMode ;
	public float startDelay;
	public float stopDelay;
	public float playTime;
}*/

public class UVMoveAnimation : MonoBehaviour 
{
	public float playSpeed = 1.0f;
	public UV_MODE_PLAY_MODE playMode = UV_MODE_PLAY_MODE.ONLY_X;
	public UV_MAT_MODE matMode = UV_MAT_MODE.WITHOUT; 
	public int fps = 30;
	public float startDelay = 0.0f;
	public float stopDelay = 0.0f;
	public float playTime = -0.1f;
	public Material material = null;
	
	
	//private bool isQueue = true;
	private float curTime = 0.0f;
	private float lastTime = 0.0f;
	//private int tileCount = 0;
	//private int curTile = 0;
	
	private bool stop = false;
	private Vector2 tempScale = Vector2.zero;
	private Vector2 tempOffset = Vector2.zero;
	
	
	void OnEnable()
	{
		if(GetComponent<Renderer>() == null || GetComponent<Renderer>().material == null)
		{
			enabled = false;
			return ;
		}
		
		Initialize();
		Invoke("StartOnTime", startDelay);
	}
	
	void OnDisable()
	{
		
	}
	
	/*
	public void Init(UVMoveStruct data, Material m)
	{
		material = m;
		renderer.material = material;
		
		if(renderer == null || renderer.material == null)
		{
			enabled = false;
			return ;
		}
		
		playSpeed = data.playSpeed;
		playMode = data.playMode;
		startDelay = data.startDelay;
		stopDelay = data.stopDelay;
		playTime = data.playTime;
		
		Initialize();
		Invoke("StartOnTime", startDelay);
	}*/
	
	private void Initialize()
	{
		if(matMode == UV_MAT_MODE.WITHOUT)
		{
			material = GetComponent<Renderer>().material;
		}
		else
		{
			if(null == material)
			{
				return;
			}
		}
	
		tempScale.x = 1.0f;
		tempScale.y = 1.0f;
		material.mainTextureScale = tempScale;
		
		//tempOffset.x = 0.0f;
		//tempOffset.y = 0.0f;
		//material.mainTextureOffset = tempOffset;
		
		stop = true;
		lastTime = Time.time;
		//curTile = 0;
		curTime = 0.0f;
		GetComponent<Renderer>().enabled = false;
	}
	
	// Update is called once per frame
	void Update ()
	{
		if(playTime != -1.0f && playTime > 0.0f)
		{
			curTime += Time.deltaTime;
			if(curTime > playTime)
			{
				curTime = 0.0f;
				Invoke("StopOnTime",stopDelay);
			}
		}
		
		if( !stop )
		{
			_UpdateFrameSequence();
		}
	}
	
	public void StartOnTime()
	{
		stop = false;
		GetComponent<Renderer>().enabled = true;
	}
	
	public void StopOnTime()
	{
		stop = true;
		GetComponent<Renderer>().enabled = false;
	}
	
	float uStart = 0.0f;
	float vStart = 0.0f;
	private void _UpdateFrameSequence()
	{
		float perFrameTime = 1.0f / fps;
		float curTime = Time.time;
		if( (curTime - lastTime) > perFrameTime )
		{
			//float xTileSize = 1.0f;
			//float yTileSize = 1.0f;
			
			if(playMode == UV_MODE_PLAY_MODE.ONLY_X)
			{
				uStart += playSpeed * perFrameTime;
				if(uStart >= 1)
				{
					uStart = 0.0f;
				}
				vStart = 0.0f;
			}
			else if(playMode == UV_MODE_PLAY_MODE.ONLY_Y)
			{
				vStart += playSpeed * perFrameTime;
				if(vStart >= 1)
				{
					vStart = 0.0f;
				}
				uStart = 0.0f;
			}
			
			tempOffset.x = uStart;
			tempOffset.y = vStart;
			
			//tempScale.x = xTileSize;
			//tempScale.y = yTileSize;
			
			//material.mainTextureScale = tempScale;
			material.mainTextureOffset = tempOffset;
			
			lastTime = curTime;
		}
	}
}
